var path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
// 使用清除插件，dist中保留本次打包文件,需要解构出来
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpack = require('webpack')
module.exports = {
    mode: 'development',
    entry: {
        // main: "./src/main.js"
        'home': './src/main.js',
        'my': "./src/my.js"
    },
    output: {
        filename: "[name]-[contenthash:8].bundle.js",
        path: path.resolve(__dirname, "dist")
    },
    module: {
        rules: [

            {
                test: /.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'less-loader'
                ],
                include: [path.resolve(__dirname, 'src')]
            },
            {
                test: /.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ],
                include: [path.resolve(__dirname, 'src')]
            },
            {
                test: /.js$/,
                use: [
                    'babel-loader'
                ],
                include: [path.resolve(__dirname, 'src')]
            },
            // {
            //     test: /.js$/,
            //     enforce: 'pre',
            //     use: [
            //         'eslint-loader'
            //     ],
            //     include: [path.resolve(__dirname, 'src')]
            // },
            {
                test: /.vue$/,
                use: [
                    'vue-loader'
                ],
                include: [path.resolve(__dirname, 'src')]
            },
            {
                test: /.vue$/,
                enforce: 'pre',
                use: [
                    'eslint-loader'
                ],
                include: [path.resolve(__dirname, 'src')]
            },
            {
                test: /\.(png|jpg|gif|jpeg|svg)/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 1024,
                        esModule: false
                    }
                }],
                include: [path.resolve(__dirname, 'src')]

            },
        ],

    },
    plugins: [
        new VueLoaderPlugin(),
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: 'vue项目webpack打包',
            source: "public/",
            inject: 'body',
            filename: "index.html",
            template: path.resolve(__dirname, "src/index.html"),
            // 设置chunk,只导入对应的打包文件
            chunks: ['home']
        }),
        new HtmlWebpackPlugin({
            title: 'vue项目webpack打包',
            source: "public/",
            inject: 'body',
            filename: "my.html",
            chunks: ['my']
        }),
        new webpack.HotModuleReplacementPlugin()
    ],
    // devtool:"cheap-module-eval-source-map",
    devtool: "eval",
    optimization: {
        // 提取公共模块
        splitChunks: {
            chunks: 'all'
        }
    },
    devServer: {
        hot: true,
        contentBase: path.join(__dirname, './src'),
        compress: true,
        port: 9000

    },
}